package com.gec.teams.wechat.common;

import cn.hutool.core.util.StrUtil;
import com.gec.teams.wechat.exception.TeamsException;
import com.gec.teams.wechat.properties.TxOssProperties;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.gse.v20191112.models.Credentials;
import com.tencentcloudapi.iai.v20180301.IaiClient;
import com.tencentcloudapi.iai.v20180301.models.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.IOException;

/**
 * 功能：
 * 作者：YumTea
 * 日期：2024/6/20 14:49
 */
@Component
@Slf4j
@Data
public class FaceComponent {

    @Autowired
    private TxOssProperties txOssProperties;
    public boolean createUser(String userName, String userId, MultipartFile file){
        try{
        // 创建Credential认证对象
        Credential cal = new Credential(txOssProperties.getAccessKey(), txOssProperties.getSecretKey());
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("iai.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        IaiClient client = new IaiClient(cal, txOssProperties.getBucket(), clientProfile);
//        实例化一个请求对象
        CreatePersonRequest request = new CreatePersonRequest();
        //准备请求数据
        request.setGroupId(txOssProperties.getGroupId());
        request.setPersonId(userId);
        request.setPersonName(userName);
        //把图像设置为64位编码
        BASE64Encoder encode = new BASE64Encoder();
            String imgStr = encode.encode(file.getBytes());
        request.setImage(imgStr);
        //返回result 看看里面有没有faceID?
            CreatePersonResponse res = client.CreatePerson(request);
            String faceId = res.getFaceId();
            if(StrUtil.isEmpty(faceId)) return false;
            return true;
        }catch (TencentCloudSDKException e){
            throw new TeamsException(e.getMessage());
        }catch (IOException e){
            throw new TeamsException("图片IO错误");
        }

    }
    //人员查询信息
    public boolean getUserInfo(String userId){
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(txOssProperties.getAccessKey(),txOssProperties.getSecretKey());
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            IaiClient client = new IaiClient(cred, txOssProperties.getBucket(), clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            GetPersonBaseInfoRequest req = new GetPersonBaseInfoRequest();
            req.setPersonId(userId);
            // 返回的resp是一个GetPers
            // onBaseInfoResponse的实例，与请求对象对应
            GetPersonBaseInfoResponse resp = client.GetPersonBaseInfo(req);
            // 输出json格式的字符串回包
            String personName = resp.getPersonName();
            if(StrUtil.isBlank(personName)){
                return false;
            }else {
                return true;
            }
        } catch (TencentCloudSDKException e) {
            throw new TeamsException("没有该模型",500);
        }
//        return false;
    }
        public VerifyPersonResponse verifyUser(String userId,MultipartFile file) {
            try {
                Credential cre = new Credential(txOssProperties.getAccessKey(), txOssProperties.getSecretKey());
                IaiClient client = new IaiClient(cre, txOssProperties.getBucket());
                VerifyPersonRequest request = new VerifyPersonRequest();
                BASE64Encoder encode = new BASE64Encoder();
                String imgstr = encode.encode(file.getBytes());
                request.setImage(imgstr);
                request.setPersonId(userId);
                return client.VerifyPerson(request);
            } catch (IOException e) {
                throw new TeamsException("IO图片错误");
            }catch (TencentCloudSDKException e) {
                throw new TeamsException(e.getMessage());
            }
        }
}
